{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "图形绘制分为形状、文字和图片三种。\n",
    "\n",
    "## 形状绘制\n",
    "\n",
    "形状包括线段、矩形、圆形和其他多边形。\n",
    "\n",
    "<img src='images/形状绘制.jpg' width=40%>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-1"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import cv2\n",
    "import numpy as np\n",
    "newImageInfo = (500,500,3)\n",
    "dst = np.zeros(newImageInfo, np.uint8)\n",
    "# 线段\n",
    "# 1 dst 2 begin point 3 end point 4 color 5 line width 6 line type\n",
    "cv2.line(dst, (100,300), (400,300), (0,255,0), 10, cv2.LINE_AA)\n",
    "# 三角形（终止位置和起始位置要相同）\n",
    "cv2.line(dst, (200,150), (50,250), (25,100,255))\n",
    "cv2.line(dst, (50,250), (400,380), (25,100,255))\n",
    "cv2.line(dst, (400,380), (200,150), (25,100,255))\n",
    "\n",
    "cv2.imshow('dst',dst)\n",
    "cv2.waitKey(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-1"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import cv2\n",
    "import numpy as np\n",
    "newImageInfo = (500,500,3)\n",
    "dst = np.zeros(newImageInfo, np.uint8)\n",
    "\n",
    "#  2 左上角 3 右下角 4 color 5 line w(-1为全填充)\n",
    "cv2.rectangle(dst, (50,100), (200,300), (255,0,0), 5)\n",
    "\n",
    "# 2 center 3 半径 4 color 5 line w\n",
    "cv2.circle(dst, (250,250), (50), (0,255,0), 2)\n",
    "\n",
    "# 2 center 3 轴长(长轴和短轴) 4 偏转角度 5 起始角度 6 终止角度 7 color 8 line w\n",
    "cv2.ellipse(dst,(256,300),(150,100),0,0,190,(255,255,0),-1)\n",
    "\n",
    "# 任意多边形，指定每个顶点的坐标\n",
    "points = np.array([[150,50],[140,140],[200,170],[250,250],[150,30]], np.int32) # (5, 2)\n",
    "# 第一个维度为点的个数\n",
    "points = points.reshape((-1,1,2)) # (5, 1, 2)\n",
    "cv2.polylines(dst, [points], True, (0,255,255)) # True表示绘制的多段线闭合\n",
    "\n",
    "cv2.imshow('dst',dst)\n",
    "cv2.waitKey(0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 文字图片绘制\n",
    "\n",
    "<img src='images/文字图片绘制.jpg' width=70%>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-1"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import cv2 \n",
    "import numpy as np\n",
    "img = cv2.imread('image0.jpg',1)\n",
    "cv2.imshow('src',img)\n",
    "# 字体\n",
    "font = cv2.FONT_HERSHEY_SIMPLEX\n",
    "# 文本框\n",
    "cv2.rectangle(img, (200,100), (500,400), (0,255,0), 3)\n",
    "text = 'this is flower'\n",
    "# 1 dst 2 文字内容 3 位置 4 字体 5 字体大小 6 颜色 7 粗细 8 line type\n",
    "cv2.putText(img, text, (100,300), font, 1, (200,100,255), 2, cv2.LINE_AA)\n",
    "cv2.imshow('src',img)\n",
    "cv2.waitKey(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-1"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 把一张图放到另一张图上\n",
    "\n",
    "import cv2 \n",
    "img = cv2.imread('image0.jpg',1)\n",
    "height = int(img.shape[0]*0.3)\n",
    "width = int(img.shape[1]*0.3)\n",
    "imgResize = cv2.resize(img, (width,height))\n",
    "\n",
    "for i in range(0, height):\n",
    "    for j in range(0, width):\n",
    "        img[i+200,j+350] = imgResize[i,j]\n",
    "cv2.imshow('src',img)\n",
    "cv2.waitKey(0)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
